package com.hotel.controllers;

import com.github.pagehelper.PageInfo;
import com.hotel.entitys.Guest;
import com.hotel.entitys.RestPOJO;
import com.hotel.services.GuestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@Api(tags="客户管理接口")//tags:说明信息
public class GuestController {
    @Autowired
    private GuestService guestService;

    @ApiOperation(
            value = "查询所有用户信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )

    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="keyWord",value="查询条件 可以为空",required=false),
            @ApiImplicitParam(paramType="query",name="curentPage",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pageSize",value="每页显示的条目数",required=true)

    })



    @GetMapping("/guest")   //分页查询
    public RestPOJO getAll(String keyWord, Integer curentPage, Integer pageSize){
        System.out.println(keyWord +curentPage+pageSize+"111111111111111111111111111111111111111111111");
        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Guest> pageInfo = guestService.getAllGuest(keyWord,curentPage,pageSize);
        if(pageInfo!=null&&pageInfo.getList().size()>0){
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(pageInfo);

        }
        else{
            restPOJO.setData(pageInfo);
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "根据id查询一条客户信息",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class  //方法返回的类型
    )
    @GetMapping("/guest/{guest_id}")//查询一条
    public RestPOJO selectById (@PathVariable("guest_id")  Integer guest_id){
        System.out.println(guest_id+"7777777777777");
        RestPOJO restPOJO = new RestPOJO();
        Guest guest = guestService.getUserByNameAndPwd(guest_id);
        if(guest!=null){
            restPOJO.setMessage("查询一条成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(guest);
        }else{
            restPOJO.setMessage("查询一条失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "增加一条客户信息",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class  //方法返回的类型
    )

    @PostMapping("/guest")//增加一条
    public RestPOJO add(@RequestBody Guest guest){
        RestPOJO restPOJO = new RestPOJO();
        int count = guestService.addGuest(guest);
        if(count>0){
          restPOJO.setMessage("增加一条成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(guest);
        }else{
            restPOJO.setMessage("查询一条失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "删除一条客户信息",
            httpMethod = "DELETE",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class  //方法返回的类型
    )
    @DeleteMapping("/guest/{guest_id}") //删除一条
    public  RestPOJO deleteById (@PathVariable("guest_id")  Integer guest_id){
        RestPOJO restPOJO = new RestPOJO();
        int  guest = guestService.delete(guest_id);

        if(guest>0) {
            restPOJO.setMessage("删除成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(guest);
        }
        else{
            restPOJO.setMessage("删除一条失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


    @ApiOperation(
            value = "修改一条客户信息 ",
            httpMethod = "PUT",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class  //方法返回的类型
    )
    @PutMapping("/guest") //修改一条
    public  RestPOJO update( @RequestBody Guest guest){
        RestPOJO restPOJO = new RestPOJO();
        int count = guestService.update(guest);
        if(count>0){
            restPOJO.setMessage("修改成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(guest);
        }else {
            restPOJO.setMessage("修改失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }


//    @ApiOperation(
//            value = "批量删除客户信息  请谨慎再谨慎操作",
//            httpMethod = "ppst",
//            protocols = "HTTP",
//            produces = "application/json",
//            response = RestPOJO.class  //方法返回的类型
//    )
//
//    @PostMapping("/guest/all") //批量删除
//    public  RestPOJO deleteAll( @RequestBody Object obj[]){
//        System.out.println(obj+"cccccccccccccccccccccccc");
//        RestPOJO restPOJO = new RestPOJO();
//        int count = guestService.deleteAll(obj);
//        if(count>0){
//            restPOJO.setMessage("删除成功");
//            restPOJO.setStatus(1000);
//            restPOJO.setData(obj);
//        }else {
//            restPOJO.setMessage("删除失败");
//            restPOJO.setStatus(1001);
//        }
//        return restPOJO;
//    }




    @ApiOperation(
            value = "客户登记插入客户表",
            httpMethod = "POST",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class  //方法返回的类型
    )
    @PostMapping("/checkin/guest")
    public  RestPOJO adds(@RequestBody Map<String,Guest> map){
        RestPOJO restPOJO = new RestPOJO();
        Guest g1 = map.get("firstGuest");
        g1.setGuest_status(1);
        Guest g2 = map.get("secondGuest");
        Guest g3 = map.get("thirdGuest");
        int a = guestService.addGuest(g1);
        if(g2.getGuest_telephone() != ""){
            g2.setGuest_status(1);
            guestService.addGuest(g2);
            a++;
        }
        if(g3.getGuest_telephone() != ""){
            g3.setGuest_status(1);
            guestService.addGuest(g3);
            a++;
        }
        if(a > 0){
            restPOJO.setMessage("增加成功");
            restPOJO.setStatus(1000);
        }else {
            restPOJO.setMessage("增加失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }
}
